// mypages/mypublish/mypublish.js
const _ = wx.cloud.database().command
Page({

    /**
     * 页面的初始数据
     */
    data: {
        lilist: [],
        lilist1: '',
        lilist2: '',
        name:'',
        userimg:'',
        
    },
    /** 
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let user = wx.getStorageSync('user');
        if (!user) {
            wx.showModal({
                title: '未登录',
                content: '确定现在去登录吗？',
                success: function (res) {
                    if (res.confirm) {
                        wx.switchTab({
                            url: '/mypages/my/my',
                        })
                    } else if (res.cancel) {
                        //console.log('点击取消了');
                        wx.switchTab({
                            url: '/mypages/my/my',
                        })

                    }
                }
            })
        }
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.mylike();
        let that = this

    },
    //获取数据
    mylike: async function () {
        let that = this;
        let user = wx.getStorageSync('user');
        let arr = [];
        let phone = user.phone;
        await wx.cloud.database().collection('my_share').where({
            phone: phone
        }).get({
            success(res) {
                console.log(res);
                that.setData({
                    name:res.data[0].name,
                    userimg:res.data[0].userimg
                })
                arr = res.data[0].issue
                that.listplay(arr);
            },
            fail(res) {
                console.log(res);
            }
        })

    },
   
    //分两列
    listplay: function (list) {
        
        let arr1 = []
        let arr2 = []
        console.log("是否结束", list);
        if (list.length > 1) {
            list.forEach((el, index) => {
                if ((index + 1) % 2 == 0) {
                    arr2 = arr2.concat(el)
                } else {
                    arr1 = arr1.concat(el)
                }
            })
            this.setData({
                lilist1: arr1,
                lilist2: arr2
            })
        } else if (list.length <= 1) {
            this.setData({
                lilist1: list
            })
        }

        console.log("arr1", arr1);
        console.log("arr2", arr2);
    },
   
    //跳转到具体文章
    urlnew:function(event){
        // console.log(event);
        let id = event.currentTarget.dataset.id
        console.log(id);
        wx.navigateTo({
            url: '/mypages/news/news?titid='+id,
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})